//  通过mutations间接更新state的多个方法

import {
  RECEIVE_GOODS,
  RECEIVE_RATINGS,
  RECEIVE_INFO,
  INCREMENT_FOOD_COUNT,
  DECREMENT_FOOD_COUNT,
  CLEAR_CART
} from './mutation-types'

import {goods,ratings,info} from '../api'
export default {
  // 获取商家信息
   async getShopInfo({commit}){
     const result = await info();
     if(result.code === 0){
       const info = result.data;
       commit(RECEIVE_INFO,{info})
     }
   },

   //获取商家评论信息
  async getShopRatings({commit},callback){
    const result = await ratings();
    if(result.code === 0){
      const ratings = result.data;
      commit(RECEIVE_RATINGS,{ratings});
      callback && callback()
    }
  },
  // 异步获取商家商品列表
  async getShopGoods({commit},callback){
    const result = await goods();
    if(result.code === 0){
      const goods = result.data;
      commit(RECEIVE_GOODS,{goods});
      // 数据更新了, 通知一下组件
      callback && callback()
    }
  },
  //同步更新count
  updataFoodCount({commit},{isAdd,food}){
   if(isAdd){
     commit(INCREMENT_FOOD_COUNT,{food})
   }else{
     commit(DECREMENT_FOOD_COUNT,{food})
   }
  },
  // 同步清空购物车
  clearCart({commit}){
    commit(CLEAR_CART)
  }
}


